package com.zhejiangzhengyuan.municipal_common.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhejiangzhengyuan.municipal_common.entity.*;
import com.zhejiangzhengyuan.municipal_common.entity.vo.GasCheckVo;
import com.zhejiangzhengyuan.municipal_common.mapper.PumpStationDeviceMappingMapper;
import com.zhejiangzhengyuan.municipal_common.mapper.PumpStationMapper;
import com.zhejiangzhengyuan.municipal_common.service.CaseService;
import com.zhejiangzhengyuan.municipal_common.service.DeviceService;
import com.zhejiangzhengyuan.municipal_common.service.PumpEstimatedValueService;
import com.zhejiangzhengyuan.municipal_common.service.WarnEventService;
import com.zhejiangzhengyuan.municipal_common.utils.ConstantUtil;
import com.zhejiangzhengyuan.municipal_common.utils.DateFormatUtil;
import com.zhejiangzhengyuan.municipal_common.utils.excelFileUtils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.zhejiangzhengyuan.municipal_common.utils.DateFormatUtil.timeFormat;

/**
 * @author :linju
 * @create on : 2020/1/2
 */
@Service
public class PumpEstimatedValueServiceImpl implements PumpEstimatedValueService {
  private final int number = 5;
  private final String warnType = ConstantUtil.WARN_TYPE.PUMPING_STATION;
  private final String moduleName = ConstantUtil.MODULE_NAME.PUMPING_STATION;
  private String departmentName = "监控中心";
  @Autowired private PumpStationMapper pumpStationMapper;
  @Autowired private CaseService caseService;
  @Autowired private PumpStationDeviceMappingMapper pumpStationDeviceMappingMapper;
  @Autowired private WarnEventService warnEventService;
  @Autowired private MongoTemplate mongoTemplate;
  @Autowired private DeviceService deviceService;

  @Override
  @Transactional(rollbackFor = Exception.class)
  public List<PumpEstimatedValue> selectPumpEstimatedValue() {
    List<PumpEstimatedValue> list = new ArrayList<>();
    // 查询舞阳泵站的液位情况
    Query query = new Query();
    Criteria criteria = new Criteria();
    criteria.and("TAG_DESC").is("污水池液位");
    criteria.and("STATION_KEY").is("3705020103010000009");
    query.addCriteria(criteria);
    PumpEstimatedValue pumpEstimatedEmptyValue;
    PumpEstimatedValue pumpEstimatedValues = mongoTemplate.findOne(query, PumpEstimatedValue.class);
    if (Objects.isNull(pumpEstimatedValues)) {
      pumpEstimatedEmptyValue = new PumpEstimatedValue();
      pumpEstimatedEmptyValue.setStationKey("3705020103010000009");
      pumpEstimatedEmptyValue.setPumpStation("舞阳路泵站");
      pumpEstimatedEmptyValue.setStatus("故障");
      pumpEstimatedEmptyValue.setValue("0");
      pumpEstimatedEmptyValue.setCode("WYLJBZ_mLit102");
      pumpEstimatedEmptyValue.setName("水池液位");
      pumpEstimatedEmptyValue.setDesc("污水池液位");
      pumpEstimatedEmptyValue.setVariableName("mLit102");
      pumpEstimatedEmptyValue.setId("5e0d58b3fe0303fb80ffd245");
      pumpEstimatedEmptyValue.setDate(new Date());
      pumpEstimatedEmptyValue.setReason("设备故障");
      list.add(pumpEstimatedEmptyValue);
      // 告警
      String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000009", "污水池液位");
      formatWarnEvent(deviceId, "设备故障", "三级");
    } else {
      QueryWrapper<PumpStationDeviceMapping> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("name", "污水池液位");
      queryWrapper.eq("code", "WYLJBZ_mLit102");
      // 获取设置的阈值
      PumpStationDeviceMapping pumpStationDeviceMapping =
          pumpStationDeviceMappingMapper.selectOne(queryWrapper);
      // 超过阈值则告警
      if (Objects .nonNull(pumpStationDeviceMapping.getValue())&&(Float.parseFloat(pumpEstimatedValues.getValue()) > Float.parseFloat(pumpStationDeviceMapping.getValue()))) {
        pumpEstimatedEmptyValue = new PumpEstimatedValue();
        pumpEstimatedEmptyValue.setStationKey("3705020103010000009");
        pumpEstimatedEmptyValue.setPumpStation("舞阳路泵站");
        pumpEstimatedEmptyValue.setStatus("故障");
        pumpEstimatedEmptyValue.setValue(pumpEstimatedValues.getValue());
        pumpEstimatedEmptyValue.setCode("WYLJBZ_mLit102");
        pumpEstimatedEmptyValue.setName("水池液位");
        pumpEstimatedEmptyValue.setDesc("污水池液位");
        pumpEstimatedEmptyValue.setVariableName("mLit102");
        pumpEstimatedEmptyValue.setId("5e0d58b3fe0303fb80ffd245");
        pumpEstimatedEmptyValue.setDate(new Date());
        pumpEstimatedEmptyValue.setReason("液位异常");
        list.add(pumpEstimatedEmptyValue);
        // 告警
        String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000009", "污水池液位");
        formatWarnEvent(deviceId, "液位异常", "二级");
      } else {
        Query query2 = new Query();
        Criteria criteria2 = new Criteria();
        criteria2.and("STATION_KEY").is("3705020103010000009");
        query2.addCriteria(criteria2);
        query2.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "故障信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValues2 =
            mongoTemplate.find(query2, PumpEstimatedValue.class);
        SelectPumpElectricity(pumpEstimatedValues, pumpEstimatedValues2);
        Query query3 = new Query();
        Criteria criteria3 = new Criteria();
        criteria3.and("STATION_KEY").is("3705020103010000009");
        query3.addCriteria(criteria3);
        query3.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "运行信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValues3 =
            mongoTemplate.find(query3, PumpEstimatedValue.class);
        boolean flag = true;
        flag = isFlag(pumpEstimatedValues, pumpEstimatedValues3, flag);
        if (flag) {
          pumpEstimatedValues.setStatus("停止");
          pumpEstimatedValues.setPumpStation("舞阳泵站");
        }
        list.add(pumpEstimatedValues);
      }
    }

    Query query1 = new Query();
    Criteria criteria1 = new Criteria();
    criteria1.and("TAG_DESC").is("雨水池液位");
    criteria1.and("STATION_KEY").is("3705020103010000009");
    query1.addCriteria(criteria1);
    PumpEstimatedValue pumpEstimatedValues1 =
        mongoTemplate.findOne(query1, PumpEstimatedValue.class);
    if (Objects.isNull(pumpEstimatedValues1)) {
      pumpEstimatedEmptyValue = new PumpEstimatedValue();
      pumpEstimatedEmptyValue.setStationKey("3705020103010000009");
      pumpEstimatedEmptyValue.setPumpStation("舞阳路泵站");
      pumpEstimatedEmptyValue.setStatus("故障");
      pumpEstimatedEmptyValue.setCode("WYLJBZ_mLit101");
      pumpEstimatedEmptyValue.setValue("0");
      pumpEstimatedEmptyValue.setName("水池液位");
      pumpEstimatedEmptyValue.setId("5e0d58f6fe0303fb80ffd246");
      pumpEstimatedEmptyValue.setVariableName("mLit101");
      pumpEstimatedEmptyValue.setDesc("雨水池液位");
      pumpEstimatedEmptyValue.setDate(new Date());
      pumpEstimatedEmptyValue.setReason("设备故障");
      list.add(pumpEstimatedEmptyValue);

      String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000009", "雨水池液位");
      formatWarnEvent(deviceId, "设备故障", "三级");
    } else {
      QueryWrapper<PumpStationDeviceMapping> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("name", "雨水池液位");
      queryWrapper.eq("code", "WYLJBZ_mLit101");
      PumpStationDeviceMapping pumpStationDeviceMapping =
          pumpStationDeviceMappingMapper.selectOne(queryWrapper);
      if (Objects.nonNull(pumpStationDeviceMapping.getValue())&&(Float.parseFloat(pumpEstimatedValues1.getValue())
          > Float.parseFloat(pumpStationDeviceMapping.getValue()))) {
        pumpEstimatedEmptyValue = new PumpEstimatedValue();
        pumpEstimatedEmptyValue.setStationKey("3705020103010000009");
        pumpEstimatedEmptyValue.setPumpStation("舞阳路泵站");
        pumpEstimatedEmptyValue.setStatus("故障");
        pumpEstimatedEmptyValue.setValue(pumpEstimatedValues1.getValue());
        pumpEstimatedEmptyValue.setCode("WYLJBZ_mLit101");
        pumpEstimatedEmptyValue.setName("水池液位");
        pumpEstimatedEmptyValue.setDesc("雨水池液位");
        pumpEstimatedEmptyValue.setVariableName("mLit101");
        pumpEstimatedEmptyValue.setId("5e0d58b3fe0303fb80ffd245");
        pumpEstimatedEmptyValue.setDate(new Date());
        pumpEstimatedEmptyValue.setReason("液位异常");
        list.add(pumpEstimatedEmptyValue);

        // 告警
        String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000009", "雨水池液位");
        formatWarnEvent(deviceId, "液位异常", "二级");
      } else {
        Query query2 = new Query();
        Criteria criteria2 = new Criteria();
        criteria2.and("STATION_KEY").is("3705020103010000009");
        query2.addCriteria(criteria2);
        query2.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "故障信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValues2 =
            mongoTemplate.find(query2, PumpEstimatedValue.class);
        SelectPumpElectricity(pumpEstimatedValues1, pumpEstimatedValues2);
        Query query3 = new Query();
        Criteria criteria3 = new Criteria();
        criteria3.and("STATION_KEY").is("3705020103010000009");
        query3.addCriteria(criteria3);
        query3.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "运行信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValues3 =
            mongoTemplate.find(query3, PumpEstimatedValue.class);
        boolean flag = true;
        flag = isFlag(pumpEstimatedValues1, pumpEstimatedValues3, flag);
        if (flag) {
          for (PumpEstimatedValue estimatedValue : list) {
            estimatedValue.setStatus("停止");
            estimatedValue.setPumpStation("舞阳泵站");
          }
        }
        list.add(pumpEstimatedValues1);
      }
    }
    // 查询304泵站的液位情况
    Query query304 = new Query();
    Criteria criteria304 = new Criteria();
    criteria304.and("TAG_DESC").is("液位");
    criteria304.and("STATION_KEY").is("3705020103010000008");
    query304.addCriteria(criteria304);
    PumpEstimatedValue pumpEstimatedValues304 =
        mongoTemplate.findOne(query304, PumpEstimatedValue.class);
    if (Objects.isNull(pumpEstimatedValues304)) {
      PumpEstimatedValue pumpEstimatedValue = new PumpEstimatedValue();
      pumpEstimatedValue.setStationKey("3705020103010000008");
      pumpEstimatedValue.setPumpStation("304泵站");
      pumpEstimatedValue.setStatus("故障");
      pumpEstimatedValue.setValue("0");
      pumpEstimatedValue.setCode("304LJBZ_slsP1");
      pumpEstimatedValue.setName("液位");
      pumpEstimatedValue.setDesc("液位");
      pumpEstimatedValue.setVariableName("slsyw1");
      pumpEstimatedValue.setId("5e0d9136fe0303fb80ffd25d");
      pumpEstimatedValue.setDate(new Date());
      pumpEstimatedValue.setReason("设备故障");
      list.add(pumpEstimatedValue);

      String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000008", "液位");
      formatWarnEvent(deviceId, "设备故障", "三级");
    } else {
      QueryWrapper<PumpStationDeviceMapping> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("name", "液位");
      queryWrapper.eq("code", "304LJBZ_slsyw1");
      PumpStationDeviceMapping pumpStationDeviceMapping =
          pumpStationDeviceMappingMapper.selectOne(queryWrapper);
      if (Objects.nonNull(pumpStationDeviceMapping.getValue())&&(Float.parseFloat(pumpEstimatedValues304.getValue())
          > Float.parseFloat(pumpStationDeviceMapping.getValue()))) {
        pumpEstimatedEmptyValue = new PumpEstimatedValue();
        pumpEstimatedEmptyValue.setStationKey("3705020103010000008");
        pumpEstimatedEmptyValue.setPumpStation("304泵站");
        pumpEstimatedEmptyValue.setStatus("故障");
        pumpEstimatedEmptyValue.setValue(pumpEstimatedValues304.getValue());
        pumpEstimatedEmptyValue.setCode("304LJBZ_slsyw1");
        pumpEstimatedEmptyValue.setName("液位");
        pumpEstimatedEmptyValue.setDesc("液位");
        pumpEstimatedEmptyValue.setVariableName("slsyw1");
        pumpEstimatedEmptyValue.setId("5e0d58b3fe0303fb80ffd245");
        pumpEstimatedEmptyValue.setDate(new Date());
        pumpEstimatedEmptyValue.setReason("液位异常");
        list.add(pumpEstimatedEmptyValue);

        // 告警
        String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000008", "液位");
        formatWarnEvent(deviceId, "液位异常", "二级");
      } else {
        Query query3042 = new Query();
        Criteria criteria3042 = new Criteria();
        criteria3042.and("STATION_KEY").is("3705020103010000008");
        query3042.addCriteria(criteria3042);
        query3042.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "故障信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValues3042 =
            mongoTemplate.find(query3042, PumpEstimatedValue.class);
        for (PumpEstimatedValue pumpEstimatedValue : pumpEstimatedValues3042) {
          if ("1".equals(pumpEstimatedValue.getValue())) {
            pumpEstimatedValues304.setStatus("故障");
            pumpEstimatedValues304.setPumpStation("304泵站");
            String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000008", "液位");
            formatWarnEvent(deviceId, "设备故障", "三级");
            break;
          }
        }
        Query query3043 = new Query();
        Criteria criteria3043 = new Criteria();
        criteria3043.and("STATION_KEY").is("3705020103010000008");
        query3043.addCriteria(criteria3043);
        query3043.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "运行信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValues3043 =
            mongoTemplate.find(query3043, PumpEstimatedValue.class);
        boolean flag1 = true;
        for (PumpEstimatedValue pumpEstimatedValue : pumpEstimatedValues3043) {
          if ("1".equals(pumpEstimatedValue.getValue())) {
            flag1 = false;
            pumpEstimatedValues304.setStatus("运行");
            pumpEstimatedValues304.setPumpStation("304泵站");
          }
        }
        if (flag1) {
          pumpEstimatedValues304.setStatus("停止");
          pumpEstimatedValues304.setPumpStation("304泵站");
        }
        list.add(pumpEstimatedValues304);
      }
    }
    // 查询春晖泵站的液位情况
    Query chunhui = new Query();
    Criteria criteriachunhui = new Criteria();
    criteriachunhui.and("TAG_DESC").is("液位");
    criteriachunhui.and("STATION_KEY").is("3705020103010000007");
    chunhui.addCriteria(criteriachunhui);
    PumpEstimatedValue pumpEstimatedValueshunhui =
        mongoTemplate.findOne(chunhui, PumpEstimatedValue.class);
    if (Objects.isNull(pumpEstimatedValueshunhui)) {
      PumpEstimatedValue pumpEstimatedValue = new PumpEstimatedValue();
      pumpEstimatedValue.setStationKey("3705020103010000007");
      pumpEstimatedValue.setPumpStation("春晖泵站");
      pumpEstimatedValue.setStatus("故障");
      pumpEstimatedValue.setValue("0");
      pumpEstimatedValue.setCode("CHLJBZ_chyw1");
      pumpEstimatedValue.setName("液位");
      pumpEstimatedValue.setDesc("液位");
      pumpEstimatedValue.setVariableName("chyw1");
      pumpEstimatedValue.setId("5e0db8f2fe0303fb80ffd269");
      pumpEstimatedValue.setDate(new Date());
      pumpEstimatedValue.setReason("设备故障");
      list.add(pumpEstimatedValue);

      String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000007", "液位");
      formatWarnEvent(deviceId, "设备故障", "三级");
    } else {
      QueryWrapper<PumpStationDeviceMapping> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("name", "液位");
      queryWrapper.eq("code", "CHLJBZ_chyw1");
      PumpStationDeviceMapping pumpStationDeviceMapping =
          pumpStationDeviceMappingMapper.selectOne(queryWrapper);
      if (Objects.nonNull(pumpStationDeviceMapping.getValue())&&(Float.parseFloat(pumpEstimatedValueshunhui.getValue())
          > Float.parseFloat(pumpStationDeviceMapping.getValue()))) {
        pumpEstimatedEmptyValue = new PumpEstimatedValue();
        pumpEstimatedEmptyValue.setStationKey("3705020103010000007");
        pumpEstimatedEmptyValue.setPumpStation("春晖泵站");
        pumpEstimatedEmptyValue.setStatus("故障");
        pumpEstimatedEmptyValue.setValue(pumpEstimatedValueshunhui.getValue());
        pumpEstimatedEmptyValue.setCode("CHLJBZ_chyw1");
        pumpEstimatedEmptyValue.setName("液位");
        pumpEstimatedEmptyValue.setDesc("液位");
        pumpEstimatedEmptyValue.setVariableName("chyw1");
        pumpEstimatedEmptyValue.setId("5e0d58b3fe0303fb80ffd245");
        pumpEstimatedEmptyValue.setDate(new Date());
        pumpEstimatedEmptyValue.setReason("液位异常");
        list.add(pumpEstimatedEmptyValue);

        // 告警
        String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000007", "液位");
        formatWarnEvent(deviceId, "液位异常", "二级");
      } else {
        Query chunhui2 = new Query();
        Criteria criteriachunhui2 = new Criteria();
        criteriachunhui2.and("STATION_KEY").is("3705020103010000007");
        chunhui2.addCriteria(criteriachunhui2);
        chunhui2.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "故障信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValueshunhui2 =
            mongoTemplate.find(chunhui2, PumpEstimatedValue.class);
        for (PumpEstimatedValue pumpEstimatedValue : pumpEstimatedValueshunhui2) {
          if ("1".equals(pumpEstimatedValue.getValue())) {
            pumpEstimatedValueshunhui.setStatus("故障");
            pumpEstimatedValueshunhui.setPumpStation("春晖泵站");
            String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000007", "液位");
            formatWarnEvent(deviceId, "设备故障", "三级");
            break;
          }
        }
        Query chunhui3 = new Query();
        Criteria criteriachunhui3 = new Criteria();
        criteriachunhui3.and("STATION_KEY").is("3705020103010000007");
        chunhui3.addCriteria(criteriachunhui3);
        chunhui3.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "运行信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValueshunhui3 =
            mongoTemplate.find(chunhui3, PumpEstimatedValue.class);
        boolean flag2 = true;
        for (PumpEstimatedValue pumpEstimatedValue : pumpEstimatedValueshunhui3) {
          if ("1".equals(pumpEstimatedValue.getValue())) {
            flag2 = false;
            pumpEstimatedValueshunhui.setStatus("运行");
            pumpEstimatedValueshunhui.setPumpStation("春晖泵站");
            break;
          }
        }
        if (flag2) {
          pumpEstimatedValueshunhui.setStatus("停止");
          pumpEstimatedValueshunhui.setPumpStation("春晖泵站");
        }
        list.add(pumpEstimatedValueshunhui);
      }
    }

    // 查询正兴泵站的液位情况
    Query zhenxing = new Query();
    Criteria criteriazhengxing = new Criteria();
    criteriazhengxing.and("TAG_DESC").is("液位");
    criteriazhengxing.and("STATION_KEY").is("3705020103010000006");
    zhenxing.addCriteria(criteriazhengxing);
    PumpEstimatedValue pumpEstimatedValueszhenxing =
        mongoTemplate.findOne(zhenxing, PumpEstimatedValue.class);
    if (Objects.isNull(pumpEstimatedValueszhenxing)) {
      PumpEstimatedValue pumpEstimatedValue = new PumpEstimatedValue();
      pumpEstimatedValue.setStationKey("3705020103010000006");
      pumpEstimatedValue.setPumpStation("振兴泵站");
      pumpEstimatedValue.setStatus("故障");
      pumpEstimatedValue.setValue("0");
      pumpEstimatedValue.setCode("ZXLJBZ_zxyw1");
      pumpEstimatedValue.setName("液位");
      pumpEstimatedValue.setDesc("液位");
      pumpEstimatedValue.setVariableName("zxyw1");
      pumpEstimatedValue.setId("5e0db9acfe0303fb80ffd274");
      pumpEstimatedValue.setDate(new Date());
      pumpEstimatedValue.setReason("设备故障");

      String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000006", "液位");
      formatWarnEvent(deviceId, "设备故障", "三级");
    } else {
      QueryWrapper<PumpStationDeviceMapping> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("name", "液位");
      queryWrapper.eq("code", "ZXLJBZ_zxyw1");
      PumpStationDeviceMapping pumpStationDeviceMapping =
          pumpStationDeviceMappingMapper.selectOne(queryWrapper);
      if (Objects.nonNull(pumpStationDeviceMapping.getValue())&&(Float.parseFloat(pumpEstimatedValueszhenxing.getValue())
          > Float.parseFloat(pumpStationDeviceMapping.getValue()))) {
        pumpEstimatedEmptyValue = new PumpEstimatedValue();
        pumpEstimatedEmptyValue.setStationKey("3705020103010000006");
        pumpEstimatedEmptyValue.setPumpStation("振兴泵站");
        pumpEstimatedEmptyValue.setStatus("故障");
        pumpEstimatedEmptyValue.setValue(pumpEstimatedValueszhenxing.getValue());
        pumpEstimatedEmptyValue.setCode("ZXLJBZ_zxyw1");
        pumpEstimatedEmptyValue.setName("液位");
        pumpEstimatedEmptyValue.setDesc("液位");
        pumpEstimatedEmptyValue.setVariableName("zxyw1");
        pumpEstimatedEmptyValue.setId("5e0d58b3fe0303fb80ffd245");
        pumpEstimatedEmptyValue.setDate(new Date());
        pumpEstimatedEmptyValue.setReason("液位异常");
        list.add(pumpEstimatedEmptyValue);

        // 告警
        String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000006", "液位");
        formatWarnEvent(deviceId, "液位异常", "二级");
      } else {
        Query zhenxing2 = new Query();
        Criteria criteriazhengxing2 = new Criteria();
        criteriazhengxing2.and("STATION_KEY").is("3705020103010000006");
        zhenxing2.addCriteria(criteriazhengxing2);
        zhenxing2.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "故障信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValueszhenxing2 =
            mongoTemplate.find(zhenxing2, PumpEstimatedValue.class);
        for (PumpEstimatedValue pumpEstimatedValue : pumpEstimatedValueszhenxing2) {
          if ("1".equals(pumpEstimatedValue.getValue())) {
            pumpEstimatedValueszhenxing.setStatus("故障");
            pumpEstimatedValueszhenxing.setPumpStation("振兴泵站");

            String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000006", "液位");
            formatWarnEvent(deviceId, "设备故障", "三级");
            break;
          }
        }
        Query zhenxing3 = new Query();
        Criteria criteriazhengxing3 = new Criteria();
        criteriazhengxing3.and("STATION_KEY").is("3705020103010000006");
        zhenxing3.addCriteria(criteriazhengxing3);
        zhenxing3.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "运行信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValueszhenxing3 =
            mongoTemplate.find(zhenxing3, PumpEstimatedValue.class);
        boolean flag3 = true;
        flag3 = isFlag3(pumpEstimatedValueszhenxing, pumpEstimatedValueszhenxing3, flag3);
        if (flag3) {
          pumpEstimatedValueszhenxing.setStatus("停止");
          pumpEstimatedValueszhenxing.setPumpStation("振兴泵站");
        }
        list.add(pumpEstimatedValueszhenxing);
      }
    }
    Query zhenxing1 = new Query();
    Criteria criteriazhengxing1 = new Criteria();
    criteriazhengxing1.and("TAG_DESC").is("桥下液位");
    criteriazhengxing1.and("STATION_KEY").is("3705020103010000006");
    zhenxing1.addCriteria(criteriazhengxing1);
    PumpEstimatedValue pumpEstimatedValueszhenxing1 =
        mongoTemplate.findOne(zhenxing1, PumpEstimatedValue.class);
    if (Objects.isNull(pumpEstimatedValueszhenxing1)) {
      PumpEstimatedValue pumpEstimatedValue = new PumpEstimatedValue();
      pumpEstimatedValue.setStationKey("3705020103010000006");
      pumpEstimatedValue.setPumpStation("振兴泵站");
      pumpEstimatedValue.setStatus("故障");
      pumpEstimatedValue.setValue("0");
      pumpEstimatedValue.setCode("ZXLJBZ_zxyw2");
      pumpEstimatedValue.setName("桥下液位");
      pumpEstimatedValue.setDesc("桥下液位");
      pumpEstimatedValue.setVariableName("zxyw2");
      pumpEstimatedValue.setId("5e0db9acfe0303fb80ffd275");
      pumpEstimatedValue.setDate(new Date());
      pumpEstimatedValue.setReason("设备故障");

      String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000006", "桥下液位");
      formatWarnEvent(deviceId, "设备故障", "三级");
    } else {
      QueryWrapper<PumpStationDeviceMapping> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("name", "桥下液位");
      queryWrapper.eq("code", "ZXLJBZ_zxyw2");
      PumpStationDeviceMapping pumpStationDeviceMapping =
          pumpStationDeviceMappingMapper.selectOne(queryWrapper);
      if (Objects.nonNull(pumpStationDeviceMapping.getValue())&&(Float.parseFloat(pumpEstimatedValueszhenxing1.getValue())
          > Float.parseFloat(pumpStationDeviceMapping.getValue()))) {
        pumpEstimatedEmptyValue = new PumpEstimatedValue();
        pumpEstimatedEmptyValue.setStationKey("3705020103010000006");
        pumpEstimatedEmptyValue.setPumpStation("振兴泵站");
        pumpEstimatedEmptyValue.setStatus("故障");
        pumpEstimatedEmptyValue.setValue(pumpEstimatedValueszhenxing1.getValue());
        pumpEstimatedEmptyValue.setCode("ZXLJBZ_zxyw2");
        pumpEstimatedEmptyValue.setName("桥下液位");
        pumpEstimatedEmptyValue.setDesc("桥下液位");
        pumpEstimatedEmptyValue.setVariableName("zxyw2");
        pumpEstimatedEmptyValue.setId("5e0d58b3fe0303fb80ffd245");
        pumpEstimatedEmptyValue.setDate(new Date());
        pumpEstimatedEmptyValue.setReason("液位异常");
        list.add(pumpEstimatedEmptyValue);

        // 告警
        String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000006", "桥下液位");
        formatWarnEvent(deviceId, "液位异常", "二级");
      } else {
        Query zhenxing2 = new Query();
        Criteria criteriazhengxing2 = new Criteria();
        criteriazhengxing2.and("STATION_KEY").is("3705020103010000006");
        zhenxing2.addCriteria(criteriazhengxing2);
        zhenxing2.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "故障信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValueszhenxing2 =
            mongoTemplate.find(zhenxing2, PumpEstimatedValue.class);
        for (PumpEstimatedValue pumpEstimatedValue : pumpEstimatedValueszhenxing2) {
          if ("1".equals(pumpEstimatedValue.getValue())) {
            pumpEstimatedValueszhenxing1.setStatus("故障");
            pumpEstimatedValueszhenxing1.setPumpStation("振兴泵站");
            String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000006", "桥下液位");
            formatWarnEvent(deviceId, "设备故障", "三级");
            break;
          }
        }
        Query zhenxing3 = new Query();
        Criteria criteriazhengxing3 = new Criteria();
        criteriazhengxing3.and("STATION_KEY").is("3705020103010000006");
        zhenxing3.addCriteria(criteriazhengxing3);
        zhenxing3.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "运行信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValueszhenxing3 =
            mongoTemplate.find(zhenxing3, PumpEstimatedValue.class);
        boolean flag3 = true;
        flag3 = isFlag3(pumpEstimatedValueszhenxing1, pumpEstimatedValueszhenxing3, flag3);
        if (flag3) {
          pumpEstimatedValueszhenxing1.setStatus("停止");
          pumpEstimatedValueszhenxing1.setPumpStation("振兴泵站");
        }
        list.add(pumpEstimatedValueszhenxing1);
      }
    }
    return list;
  }

  private boolean isFlag3(
      PumpEstimatedValue pumpEstimatedValueszhenxing,
      List<PumpEstimatedValue> pumpEstimatedValueszhenxing3,
      boolean flag3) {
    for (PumpEstimatedValue pumpEstimatedValue : pumpEstimatedValueszhenxing3) {
      if ("0".equals(pumpEstimatedValue.getValue())) {
        flag3 = false;
        pumpEstimatedValueszhenxing.setStatus("运行");
        pumpEstimatedValueszhenxing.setPumpStation("振兴泵站");
        break;
      }
    }
    return flag3;
  }
  private void formatWarnEvent(String deviceId, String reason, String level) {
    Device device = deviceService.getById(Long.parseLong(deviceId));
    if (Objects.nonNull(device) && !device.isWarn()) {
      device.setWarn(true);
      deviceService.updateDevice(device);
      WarnEvent warnEvent = new WarnEvent();
      warnEvent
          .setDataOrigin(ConstantUtil.WARN_DATA_ORIGIN.SYSTEM)
          .setDeviceId(Long.valueOf(deviceId))
          .setType(warnType)
          .setLevel(level)
          .setReason(reason)
          .setReportUserInfoId((long) 1)
          .setReset(false)
          .setSendOrder(false)
          .setCreateTime(timeFormat(System.currentTimeMillis()))
          .setUpdateTime(timeFormat(System.currentTimeMillis()));
      // 添加告警消息
      warnEventService.insertWarnEvent(moduleName, warnEvent, warnType, departmentName);
    }
  }

  // 舞阳泵站的电流显示
  @Override
  @Transactional(rollbackFor = Exception.class)
  public List<PumpEstimatedValue> selectPumpElectricityValue() {
    // 查询舞阳泵站的电流情况
    Query query = new Query();
    Criteria criteria = new Criteria();
    criteria.and("TAG_NAME").is("电流");
    criteria.and("STATION_KEY").is("3705020103010000009");
    query.addCriteria(criteria);
    List<PumpEstimatedValue> pumpEstimatedValues =
        mongoTemplate.find(query, PumpEstimatedValue.class);
    if (pumpEstimatedValues.size() < number) {
      PumpEstimatedValue pumpEstimatedValue = new PumpEstimatedValue();
      pumpEstimatedValue.setStationKey("3705020103010000009");
      pumpEstimatedValue.setPumpStation("舞阳路泵站");
      pumpEstimatedValue.setStatus("故障");
      pumpEstimatedValue.setValue("0");
      pumpEstimatedValue.setCode("WYLJBZ_mY1A");
      pumpEstimatedValue.setName("水泵电流");
      pumpEstimatedValue.setDesc("水泵电流");
      pumpEstimatedValue.setVariableName("mLit102");
      pumpEstimatedValue.setId("5e0d590afe0303fb80ffd247");
      pumpEstimatedValue.setDate(new Date());
      pumpEstimatedValue.setReason("设备故障");

      String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000009", "泵站故障信号");
      formatWarnEvent(deviceId, "设备故障", "三级");
    }

    List<PumpEstimatedValue> list = new ArrayList<>(pumpEstimatedValues);
    Query query2 = new Query();
    Criteria criteria2 = new Criteria();
    criteria2.and("STATION_KEY").is("3705020103010000009");
    query2.addCriteria(criteria2);
    query2.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "故障信号" + ".*"));
    List<PumpEstimatedValue> pumpEstimatedValues2 =
        mongoTemplate.find(query2, PumpEstimatedValue.class);
    // 抽取重复代码
    for (PumpEstimatedValue pumpEstimatedValue : list) {
      SelectPumpElectricity(pumpEstimatedValue, pumpEstimatedValues2);
    }
    Query query3 = new Query();
    Criteria criteria3 = new Criteria();
    criteria3.and("STATION_KEY").is("3705020103010000009");
    query3.addCriteria(criteria3);
    query3.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "运行信号" + ".*"));
    List<PumpEstimatedValue> pumpEstimatedValues3 =
        mongoTemplate.find(query3, PumpEstimatedValue.class);
    boolean flag = true;
    for (PumpEstimatedValue pumpEstimatedValue : list) {
      flag = isFlag(pumpEstimatedValue, pumpEstimatedValues3, flag);
    }
    if (flag) {
      for (PumpEstimatedValue estimatedValue : list) {
        estimatedValue.setStatus("停止");
        estimatedValue.setPumpStation("舞阳泵站");
      }
    }
    Query query4 = new Query();
    Criteria criteria4 = new Criteria();
    criteria4.and("STATION_KEY").is("3705020103010000009");
    query4.addCriteria(criteria4);
    query4.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "控制模式" + ".*"));
    List<PumpEstimatedValue> pumpEstimatedValues4 =
        mongoTemplate.find(query4, PumpEstimatedValue.class);
    for (PumpEstimatedValue pumpEstimatedValue : pumpEstimatedValues4) {
      if ("1".equals(pumpEstimatedValue.getValue())) {
        for (PumpEstimatedValue estimatedValue : list) {
          estimatedValue.setControl("自动");
        }
      }
      if ("0".equals(pumpEstimatedValue.getValue())) {
        for (PumpEstimatedValue estimatedValue : list) {
          estimatedValue.setControl("手动");
        }
      }
    }
    return list;
  }

  private void SelectPumpElectricity(
      PumpEstimatedValue pumpEstimatedValues, List<PumpEstimatedValue> pumpEstimatedValues2) {
    for (PumpEstimatedValue pumpEstimatedValue : pumpEstimatedValues2) {
      if ("1".equals(pumpEstimatedValue.getValue())) {
        pumpEstimatedValues.setStatus("故障");
        pumpEstimatedValues.setPumpStation("舞阳泵站");
        String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000009", "泵站故障信号");
        formatWarnEvent(deviceId, "设备故障", "三级");
        break;
      }
    }
  }

  // 查询泵站气体的数据
  @Override
  @Transactional(rollbackFor = Exception.class)
  public List<GasCheckVo> selectPumpGasValue() {
    List<GasCheckVo> list = new ArrayList<>();
    // 查询304泵站的气体情况
    Query query = new Query();
    Criteria criteria = new Criteria();
    criteria.and("TAG_DESC").is("硫化氢");
    criteria.and("STATION_KEY").is("3705020103010000008");
    query.addCriteria(criteria);
    PumpEstimatedValue one =
        mongoTemplate.findOne(query, PumpEstimatedValue.class, "ROBIN_HWSERVICE_CONFIG");
    // 如果数据查询为空，则说明设备出了问题
    // 如果查询的值大于0，则说明气体异常
    SelectPumpGasValue8(one);
    Query query1 = new Query();
    Criteria criteria1 = new Criteria();
    criteria1.and("TAG_DESC").is("氨气");
    criteria1.and("STATION_KEY").is("3705020103010000008");
    query1.addCriteria(criteria1);
    PumpEstimatedValue two =
        mongoTemplate.findOne(query, PumpEstimatedValue.class, "ROBIN_HWSERVICE_CONFIG");
    SelectPumpGasValue8(two);
    GasCheckVo gasCheckVo = new GasCheckVo();
    gasCheckVo.setCode("3705020103010000008");
    gasCheckVo.setPumpStation("304泵站");
    gasCheckVo.setDate(Objects.isNull(two) ? null :two.getDate());
    gasCheckVo.setH2S(Objects.isNull(one) ? null :one.getValue());
    gasCheckVo.setNH3(Objects.isNull(two) ? null :two.getValue());
    list.add(gasCheckVo);
    // 查询春晖泵站的气体情况
    Query query2 = new Query();
    Criteria criteria2 = new Criteria();
    criteria2.and("TAG_DESC").is("硫化氢");
    criteria2.and("STATION_KEY").is("3705020103010000007");
    query2.addCriteria(criteria2);
    PumpEstimatedValue three =
        mongoTemplate.findOne(query2, PumpEstimatedValue.class, "ROBIN_HWSERVICE_CONFIG");
    // three为空设备故障，大于0 气体报警
    SelectPumpGasValue7(three);

    Query query3 = new Query();
    Criteria criteria3 = new Criteria();
    criteria3.and("TAG_DESC").is("氨气");
    criteria3.and("STATION_KEY").is("3705020103010000007");
    query3.addCriteria(criteria1);
    PumpEstimatedValue four =
        mongoTemplate.findOne(query3, PumpEstimatedValue.class, "ROBIN_HWSERVICE_CONFIG");
    SelectPumpGasValue7(four);
    GasCheckVo gasCheckVo1 = new GasCheckVo();
    gasCheckVo1.setCode("3705020103010000007");
    gasCheckVo1.setPumpStation("春晖泵站");
    gasCheckVo1.setDate(Objects.isNull(four) ? null : four.getDate());
    gasCheckVo1.setH2S(Objects.isNull(three) ? null : three.getValue());
    gasCheckVo1.setNH3(Objects.isNull(four) ? null : four.getValue());
    list.add(gasCheckVo1);
    // 查询振兴泵站的气体情况
    Query query4 = new Query();
    Criteria criteria4 = new Criteria();
    criteria4.and("TAG_DESC").is("硫化氢");
    criteria4.and("STATION_KEY").is("3705020103010000006");
    query4.addCriteria(criteria4);
    PumpEstimatedValue five =
        mongoTemplate.findOne(query4, PumpEstimatedValue.class, "ROBIN_HWSERVICE_CONFIG");
    // 如果数据查询为空，则说明设备出了问题
    // 如果查询的值大于0，则说明气体异常
    SelectPumpGasValue6(five);
    Query query5 = new Query();
    Criteria criteria5 = new Criteria();
    criteria5.and("TAG_DESC").is("氨气");
    criteria5.and("STATION_KEY").is("3705020103010000006");
    query5.addCriteria(criteria5);
    PumpEstimatedValue six =
        mongoTemplate.findOne(query5, PumpEstimatedValue.class, "ROBIN_HWSERVICE_CONFIG");
    SelectPumpGasValue6(six);
    GasCheckVo gasCheckVo2 = new GasCheckVo();
    gasCheckVo2.setCode("3705020103010000006");
    gasCheckVo2.setPumpStation("振兴泵站");
    gasCheckVo2.setDate(Objects.isNull(five) ? null :five.getDate());
    gasCheckVo2.setH2S(Objects.isNull(five) ? null :five.getValue());
    gasCheckVo2.setNH3(Objects.isNull(six) ? null :six.getValue());
    list.add(gasCheckVo2);
    return list;
  }

  // 导出泵站液位值
  @Override
  @Transactional(rollbackFor = Exception.class)
  public void exportPumpEstimatedValue(HttpServletResponse response) {
    List<PumpEstimatedValue> list = selectPumpEstimatedValue();
    for (PumpEstimatedValue pumpEstimatedValue : list) {
      if (pumpEstimatedValue.getReason() == null) {
        pumpEstimatedValue.setReason("--");
      }
    }
    Set<String> exclude = new HashSet<>();
    exclude.add("id");
    exclude.add("code");
    exclude.add("desc");
    exclude.add("name");
    exclude.add("longitude");
    exclude.add("latitude");
    exclude.add("variableName");
    exclude.add("control");
    ExcelUtil.objectToExcelAndDownload(response, exclude, "liquid", PumpEstimatedValue.class, list);
  }

  // 导出泵站电流值
  @Override
  public void exportPumpElectricityValue(HttpServletResponse response) {
    List<PumpEstimatedValue> list = selectPumpElectricityValue();
    for (PumpEstimatedValue pumpEstimatedValue : list) {
      pumpEstimatedValue.setVariableName(pumpEstimatedValue.getValue());
      if (pumpEstimatedValue.getReason() == null) {
        pumpEstimatedValue.setReason("--");
      }
    }
    Set<String> exclude = new HashSet<>();
    exclude.add("id");
    exclude.add("code");
    exclude.add("desc");
    exclude.add("longitude");
    exclude.add("latitude");
    exclude.add("name");
    exclude.add("value");

    ExcelUtil.objectToExcelAndDownload(
        response, exclude, "electricity", PumpEstimatedValue.class, list);
  }

  // 导出气体的值
  @Override
  public void exportPumpGasValue(HttpServletResponse response) {
    List<GasCheckVo> list = selectPumpGasValue();
    ExcelUtil.objectToExcelAndDownload(response, null, "gas", GasCheckVo.class, list);
  }

  // 模糊查询泵站检测的气体
  @Override
  public List<GasCheckVo> selectVaguePumpGasValue(String name, String startTime, String endTime)
      throws ParseException {
    long timeBystartTime = DateFormatUtil.getTimeByString("yyyy-MM-dd", startTime);
    long timeByendTime = DateFormatUtil.getTimeByString("yyyy-MM-dd", endTime);
    List<GasCheckVo> voList = new ArrayList<>();
    List<GasCheckVo> list = selectPumpGasValue();
    for (GasCheckVo gasCheckVo : list) {
      DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd");
      String format = dFormat.format(gasCheckVo.getDate());
      long time = DateFormatUtil.getTimeByString("yyyy-MM-dd", format);
      if (time > timeBystartTime
          && time < timeByendTime
          && gasCheckVo.getPumpStation().equals(name)) {
        voList.add(gasCheckVo);
      }
    }
    return voList;
  }
  // 模糊查询泵站检测的电流
  @Override
  public List<PumpEstimatedValue> selectVaguePumpElectricityValue(
      String name, String startTime, String endTime) throws ParseException {
    long timeBystartTime = DateFormatUtil.getTimeByString("yyyy-MM-dd", startTime);
    long timeByendTime = DateFormatUtil.getTimeByString("yyyy-MM-dd", endTime);
    List<PumpEstimatedValue> voList = new ArrayList<>();
    List<PumpEstimatedValue> list = selectPumpElectricityValue();
    SelectVague(name, timeBystartTime, timeByendTime, voList, list);
    return voList;
  }

  private void SelectVague(
      String name,
      long timeBystartTime,
      long timeByendTime,
      List<PumpEstimatedValue> voList,
      List<PumpEstimatedValue> list)
      throws ParseException {
    for (PumpEstimatedValue pumpEstimatedValue : list) {
      DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String format = dFormat.format(pumpEstimatedValue.getDate());
      long time = DateFormatUtil.getTimeByString("yyyy-MM-dd", format.split(" ")[0]);
      if (time > timeBystartTime
          && time < timeByendTime
          && pumpEstimatedValue.getPumpStation().equals(name)) {
        voList.add(pumpEstimatedValue);
      }
    }
  }

  // 模糊查询泵站检测的液位
  @Override
  public List<PumpEstimatedValue> selectVaguePumpEstimatedValue(
      String name, String startTime, String endTime) throws ParseException {
    long timeBystartTime = DateFormatUtil.getTimeByString("yyyy-MM-dd", startTime);
    long timeByendTime = DateFormatUtil.getTimeByString("yyyy-MM-dd", endTime);
    List<PumpEstimatedValue> voList = new ArrayList<>();
    List<PumpEstimatedValue> list = selectPumpEstimatedValue();
    SelectVague(name, timeBystartTime, timeByendTime, voList, list);
    return voList;
  }

  // 查询泵站过去24小时设备故障次数
  @Override
  @Transactional(rollbackFor = Exception.class)
  public List getDeviceBreakdownCount() {
    List list = new ArrayList();
    QueryWrapper<PumpStation> queryWrapper = new QueryWrapper<>();
    queryWrapper.like("name", "泵站");
    List<PumpStation> pumpStations = pumpStationMapper.selectList(queryWrapper);
    for (PumpStation pumpStation : pumpStations) {
      Calendar calendar = Calendar.getInstance();
      calendar.add(Calendar.DATE, -1);
      String startTime;
      String endTime;
      long nextTime = calendar.getTimeInMillis();
      JSONArray result = new JSONArray();
      for (int i = 1; i <= 12; i++) {
        startTime = DateFormatUtil.getStringByTime("yyyy-MM-dd HH:mm:ss", nextTime);
        // 时间间隔两小时
        calendar.add(Calendar.HOUR_OF_DAY, 2);
        nextTime = calendar.getTimeInMillis();
        endTime = DateFormatUtil.getStringByTime("yyyy-MM-dd HH:mm:ss", nextTime - 1);
        Integer waterValueByDeviceCode =
            pumpStationMapper.getDeviceBreakdownCount(pumpStation.getCode(), startTime, endTime);
        JSONObject object = new JSONObject();
        object.put("x", i * 2);
        object.put("y", waterValueByDeviceCode == null ? 0 : waterValueByDeviceCode);
        result.add(object);
      }
      list.add(result);
    }

    return list;
  }

  // 查询泵站过去24小时设备液位变化次数
  @Override
  @Transactional(rollbackFor = Exception.class)
  public List getDeviceAnalyzeCount() {
    List list = new ArrayList();
    QueryWrapper<PumpStation> queryWrapper = new QueryWrapper<>();
    queryWrapper.like("name", "泵站");
    List<PumpStation> pumpStations = pumpStationMapper.selectList(queryWrapper);
    for (PumpStation pumpStation : pumpStations) {
      Calendar calendar = Calendar.getInstance();
      calendar.add(Calendar.DATE, -1);
      String startTime;
      String endTime;
      long nextTime = calendar.getTimeInMillis();
      JSONArray result = new JSONArray();
      for (int i = 1; i <= 12; i++) {
        startTime = DateFormatUtil.getStringByTime("yyyy-MM-dd HH:mm:ss", nextTime);
        // 时间间隔两小时
        calendar.add(Calendar.HOUR_OF_DAY, 2);
        nextTime = calendar.getTimeInMillis();
        endTime = DateFormatUtil.getStringByTime("yyyy-MM-dd HH:mm:ss", nextTime - 1);
        Integer waterValueByDeviceCode =
            pumpStationMapper.getDeviceAnalyzeCount(pumpStation.getCode(), startTime, endTime);
        JSONObject object = new JSONObject();
        object.put("x", i * 2);
        object.put("y", waterValueByDeviceCode == null ? 0 : waterValueByDeviceCode);
        result.add(object);
      }
      list.add(result);
    }

    return list;
  }

  private void SelectPumpGasValue6(PumpEstimatedValue five) {
    String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000006", "液位");
    if (Objects.isNull(five)) {
      formatWarnEvent(deviceId, "设备故障", "三级");
    } else if (Float.parseFloat(five.getValue()) > 0) {
      formatWarnEvent(deviceId, "数据异常", "二级");
    }
  }

  private void SelectPumpGasValue7(PumpEstimatedValue three) {
    String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000007", "液位");
    if (Objects.isNull(three)) {
      formatWarnEvent(deviceId, "设备故障", "三级");
    } else if (Float.parseFloat(three.getValue()) > 0) {
      formatWarnEvent(deviceId, "数据异常", "二级");
    }
  }

  private void SelectPumpGasValue8(PumpEstimatedValue one) {
      String deviceId = pumpStationMapper.getPumpDeviceId("3705020103010000008", "液位");
      if (Objects.isNull(one)) {
        formatWarnEvent(deviceId, "设备故障", "三级");
      } else if (Float.parseFloat(one.getValue()) > 0) {
        formatWarnEvent(deviceId, "数据异常", "二级");
      }
  }

  private boolean isFlag(
      PumpEstimatedValue pumpEstimatedValues,
      List<PumpEstimatedValue> pumpEstimatedValues3,
      boolean flag) {
    for (PumpEstimatedValue pumpEstimatedValue : pumpEstimatedValues3) {
      if ("1".equals(pumpEstimatedValue.getValue())) {
        flag = false;
        pumpEstimatedValues.setStatus("运行");
        pumpEstimatedValues.setPumpStation("舞阳泵站");
        break;
      }
    }
    return flag;
  }
}
